﻿using JetBrains.Annotations;
using MyLifeVn.AddThis.Models;
using Orchard;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement;
using Orchard.Core.Routable.Models;

namespace MyLifeVn.AddThis.Drivers
{
    [UsedImplicitly]
    public class AddThisPartDriver : ContentPartDriver<AddThisPart>
    {
        private readonly IOrchardServices services;

        public AddThisPartDriver(IOrchardServices services)
        {
            this.services = services;
        }

        protected override DriverResult Display(AddThisPart part, string displayType, dynamic shapeHelper)
        {
            var shareSettings = services.WorkContext.CurrentSite.As<AddThisSettingsPart>();

            if (shareSettings == null || string.IsNullOrWhiteSpace(shareSettings.Script))
            {
                return null;
            }

            var routePart = part.As<RoutePart>();
            if (!string.IsNullOrEmpty(routePart.Title) && !string.IsNullOrEmpty(routePart.Slug))
            {
                return ContentShape("Parts_AddThis", () => shapeHelper.Parts_AddThis(AddThisScript: shareSettings.Script));    
            }

            return null;
        }

        protected override DriverResult Editor(AddThisPart part, dynamic shapeHelper)
        {
            return null;
        }
    }
}